package permission

import (
	"context"
	"errors"
	"fmt"
	"private-domain-overseas-service/admin/business/api/internal/repository/permission"
	"private-domain-overseas-service/admin/business/api/internal/svc"
	"private-domain-overseas-service/admin/business/api/internal/types"
	"private-domain-overseas-service/pkg/common"
	"private-domain-overseas-service/pkg/status/bizadmin"
	"private-domain-overseas-service/pkg/xerr"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/adminrole"
	"private-domain-overseas-service/tools/ent/predicate"

	"github.com/zeromicro/go-zero/core/logx"
)

type AdminRoleEditLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewAdminRoleEditLogic - 后台角色编辑接口
func NewAdminRoleEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminRoleEditLogic {
	return &AdminRoleEditLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// AdminRoleEdit - 后台角色编辑接口
func (l *AdminRoleEditLogic) AdminRoleEdit(req *types.AdminRoleEditRequest) error {
	adminRoleDAO := permission.NewAdminRoleDao(l.ctx, l.svcCtx)

	// 不能创建超级管理员角色
	if req.AdminRoleType == bizadmin.TypeSuper {
		return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, "不能操作超级管理员角色")
	}

	err := common.WithTx(l.ctx, l.svcCtx.Orm, func(tx *ent.Tx) error {
		// 新增
		if req.OperateType == bizadmin.TypeAdd {
			// 查询员工角色
			adminRoleCond := make([]predicate.AdminRole, 0, 1)
			adminRoleCond = append(adminRoleCond, adminrole.AdminRoleTypeEQ(bizadmin.TypeNormal))
			adminRoleCond = append(adminRoleCond, adminrole.IsDeleteEQ(bizadmin.NotDelete))
			adminRoleStaff, err := adminRoleDAO.AdminRoleFirst(adminRoleCond)
			if err != nil {
				return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, "查询员工角色出错")
			}

			// 创建角色
			request := ent.AdminRole{
				AdminRoleName:      req.AdminRoleName,
				AdminRoleRouterIds: adminRoleStaff.AdminRoleRouterIds,
				AdminRoleDesc:      req.AdminRoleDesc,
				AdminRoleType:      req.AdminRoleType,
			}
			err = adminRoleDAO.AdminRoleCreate(&request, tx)
			if err != nil {
				return errors.New(fmt.Sprintf("创建角色失败: %v", err.Error()))
			}
		}

		// 更新
		if req.OperateType == bizadmin.TypeEdit {
			if req.ID == 0 {
				return xerr.ErrCodeMsg(xerr.ServerRequestParamError, "角色id为空")
			}

			// 查询要编辑的角色
			adminRoleCond := make([]predicate.AdminRole, 0, 1)
			adminRoleCond = append(adminRoleCond, adminrole.IDEQ(int(req.ID)))
			adminRole, err := adminRoleDAO.AdminRoleFirst(adminRoleCond)
			if err != nil {
				return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, "查询员工角色出错")
			}

			// 如果本来是普通用户，但是要改为其他角色类型，返回错误
			if adminRole.AdminRoleType == bizadmin.TypeNormal && req.AdminRoleType != bizadmin.TypeNormal {
				return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, "普通用户角色不能修改为非普通用户角色")
			}

			// 更新角色
			request := ent.AdminRole{
				ID:                 int(req.ID),
				AdminRoleName:      req.AdminRoleName,
				AdminRoleRouterIds: req.AdminRoleRouterIds,
				AdminRoleDesc:      req.AdminRoleDesc,
				AdminRoleType:      req.AdminRoleType,
			}
			err = adminRoleDAO.AdminRoleUpdate(&request, tx)
			if err != nil {
				return errors.New(fmt.Sprintf("更细角色失败: %v", err.Error()))
			}
		}
		return nil
	})

	if err != nil {
		return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, err.Error())
	}

	return nil
}
